package com.example.easybuy.service.impl;

import com.example.easybuy.entity.Like;
import com.example.easybuy.service.LikeService;
import com.example.easybuy.util.AjaxResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public AjaxResult addCollection(Like like, String loginName) {
        String key = loginName + "-like";
        List<Like> likes = new ArrayList<>();
//        Map<Integer, Like> likes = new HashMap<>();
        if (stringRedisTemplate.opsForValue().get(key) != null) {
            likes = this.getLikes(loginName);
            for (Like like1 : likes
            ) {
                if (like1.getProductId() == like.getProductId()) {
                    return AjaxResult.success().add("likes", likes);
                }
            }
            if (likes.size() == 5) {
                likes.remove(0);
            }
        }
        likes.add(like);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String likesJson = objectMapper.writeValueAsString(likes);
            stringRedisTemplate.opsForValue().set(key, likesJson);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return AjaxResult.success().add("likes", likes);
    }

    @Override
    public List<Like> getLikes(String loginName) {
        String key = loginName + "-like";
        List<Like> likes = new ArrayList<>();
        if (stringRedisTemplate.opsForValue().get(key) != null) {
            String likesJson = stringRedisTemplate.opsForValue().get(key);
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                likes = objectMapper.readValue(likesJson, new TypeReference<List<Like>>() {
                });
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        return likes;
    }

    @Override
    public boolean delAll(String loginName) {
        String key = loginName + "-like";
        try {
            stringRedisTemplate.delete(key);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delLike(int productId, String loginName) {

        String key = loginName + "-like";
        List<Like> likes = new ArrayList<>();
        String likesJson = stringRedisTemplate.opsForValue().get(key);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            likes = objectMapper.readValue(likesJson, new TypeReference<List<Like>>() {
            });
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        likes.removeIf(like -> like.getProductId() == productId);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String likeJson = mapper.writeValueAsString(likes);
            stringRedisTemplate.opsForValue().set(key, likeJson);
            return true;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return false;
        }

    }
}
